package org.noote.libs.computervision.detection.movement;

import org.noote.libs.computervision.Intensity;
import org.noote.libs.computervision.buffer.Buffer8888XRGB;
import org.noote.libs.computervision.buffer.Buffer8I;

public class MovementDetection_Map extends Buffer8I {

	float _fTime = 0.0f, _fGhostEffect = 0.00f;
	Buffer8I _bufferRef = new Buffer8I();
	
	Intensity _bufferIntensityTemp = new Intensity();
	Intensity _bufferDiffTemp = new Intensity();
	
	public boolean push(Buffer8888XRGB xrgb, float fTimeSlice)
	{
		// make a intensity image from rgb
		if(!_bufferIntensityTemp.calculIntensityFrom(xrgb)) return false;
		
		// this is the first time ?
		if(_fTime == 0)
		{
			// just create size image
			this.createAs(_bufferIntensityTemp);
			// set black :)
			this.fill(0);
			
			// and push ref image
			_bufferRef.createFrom(_bufferIntensityTemp);
			
			_fTime += fTimeSlice;
			return true;
		}
		
		// create map diff (intensity - previous one as ref)
		_bufferDiffTemp.createFrom(_bufferRef);
		_bufferDiffTemp.diff(_bufferIntensityTemp);
		
		// add ghost effect
		if(_fGhostEffect>0.0f && fTimeSlice<_fGhostEffect)
		{
			// lowered current map
			this.mult((_fGhostEffect-fTimeSlice)/_fGhostEffect);
		}
		else
		{
			// set black :)
			this.fill(0);
		}
		// add new diff
		this.add(_bufferDiffTemp);
		
		// ref as current intensity :)
		_bufferRef.putBuffer(0, _bufferIntensityTemp.getBuffer());
		
		_fTime += fTimeSlice;
		return true;
	}	
}
